# -*- encoding=utf8 -*-

__author__ = "code哥"
import subprocess
from sanic.blueprints import Blueprint
from sanic.views import HTTPMethodView
from sanic import response
from sanic_openapi import doc

blueprint = Blueprint(__name__, '/api')


class RYMStop(HTTPMethodView):

    @doc.summary("停止单个媷羊毛脚本程序")
    @doc.consumes({"view": {"script": str, "device": str}}, location="body")
    def post(self, request):
        script = request.json.get('script', '')
        device = request.json.get('device', '')
        command = "ps -ef | grep %s.py | grep %s | grep -v grep | awk '{print $2}' | xargs kill -TERM" % (script, device,)
        process = subprocess.Popen(command,
                                   shell=True, close_fds=True, stdin=subprocess.PIPE,
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE)
        process.wait()
        return response.json({'code': 0, 'data': {'msg': '停止成功'}})


blueprint.add_route(RYMStop.as_view(), '/rym_single_stop', strict_slashes=True)
